<?php
	error_reporting(1);

	define('IN_IA', true);
	define('IA_ROOT_WEB', str_replace("\\",'/', dirname(__FILE__)));
	ini_set('display_errors', 'on');
	
	Workerman\Protocols\Http::sessionStart();
	$_SESSION['MES_URL']="http://".$_SERVER["SERVER_NAME"].":".DEBUG_PORT;

    $_SESSION['MES_WEB_URL']= "http://".$_SERVER["SERVER_NAME"].":".INTERFACE_WEB_PORT;
	$_SESSION['LOGGER_URL']="http://".$_SERVER["SERVER_NAME"].":".LOGGER_PORT;
	
	$_SESSION['IA_ROOT_WEB']=IA_ROOT_WEB;
	$_SESSION['login_token'] = mt_rand(10000000,99999999);
?>

<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>HF+ MES</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="font-awesome/css/font-awesome.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

</head>

<body class="gray-bg">

    <div class="middle-box text-center loginscreen animated fadeInDown">
        <div>
            <div>

                <h1 class="logo-name">HF+</h1>
            </div>
            <h3>Welcome to CMS</h3>
			<p id="msg">
				
			</p>
            <form class="m-t" role="form" action="login.php">
                <div class="form-group">
                    <input  class="form-control" placeholder="Username" required=""  name="username" id="username"/>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="Password" required=""  name="password" id="password"/>
                </div>
				<input type="hidden" name="token" value="<?php echo $_SESSION['login_token'];?>" id="token"/> 
                <button type="button" class="btn btn-primary block full-width m-b" id="submit">Login</button>
            </form>
            <p class="m-t"> <small>
				Power by Blue-Key Electronic Industry Co., Ltd<br/>
				Copyright &copy;Guangzhou Suichuang Information Tech Ltd
			</small> </p>
        </div>
    </div>

    <!-- Mainly scripts -->
    <script src="js/jquery-2.1.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
	<script>
		$('#password').bind('keypress',function(event){  
			if(event.keyCode == "13")      
			{  
				do_login();
			}  
		});
		
		$("#submit").click(function (){
			do_login();
		});
		
		function do_login(){
			var data = JSON.stringify({"action":"get_token", "param":{username:$('#username').val(), password:$('#password').val()}});
			$.post("<?php echo $_SESSION['MES_URL'];?>",data,function(result){
				try{
					var result = JSON.parse(result);
				}catch(err){
					$("#msg").html("登录失败,返回数据异常");
					$("#username").val("");
					$("#password").val("");
					return;
				}
				if(result["errcode"] !=0){
					$("#msg").html("登录失败," + result["errmsg"]);
					$("#username").val("");
					$("#password").val("");
				}else{
					location.href = "main.php?access_token="+result["rtval"]["access_token"]+"";
				}
			});
		}
	</script>
</body>

</html>
